package com.java;

import org.junit.Test;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.*;

public class Java9Test1 {
    //java8中的写法：
    @Test
    public void test1(){
        List<String> namesList = new ArrayList<>();
        namesList.add("Joe");
        namesList.add("Bob");
        namesList.add("Bill");
        //返回的namesList是一个只读的集合
        namesList = Collections.unmodifiableList(namesList);
//        namesList.add("Tom");
        System.out.println(namesList);
    }

    @Test
    public void test2() {
        List<String> list = Collections.unmodifiableList(Arrays.asList("a", "b", "c"));
        Set<String> set = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("a", "b", "c")));
        // 如下操作不适用于jdk 8 及之前版本,适用于jdk 9
        Map<Object, Object> map = Collections.unmodifiableMap(new HashMap<>() {
            {
                put("a", 1);
                put("b", 2);
                put("c", 3);
            }
        });
        map.forEach((k,v) -> System.out.println(k+":"+v));
    }

    @Test
    public void test3() {
        //此时得到的集合list也是一个只读集合。
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        //UnsupportedOperationException
        list.add(6);
        System.out.println(list);
    }

    //java9新特性八：集合工厂方法：创建只读集合
    @Test
    public void test4() {
        List<Integer> list1 = List.of(1, 2, 3, 4, 5);
        //UnsupportedOperationException
//        list1.add(6);
        System.out.println(list1);
        Set<Integer> set = Set.of(23, 3, 54, 65, 43, 76, 87, 34, 46);
        //UnsupportedOperationException
//        set.add(23);
        System.out.println(set);

        Map<String, Integer> map = Map.of("Tom", 23, "Jrry", 54, "HanMeimei", 12);
//        map.put("Jary",12);
        System.out.println(map);

        Map<String, Integer> map1 = Map.ofEntries(Map.entry("Tom", 34), Map.entry("Jary", 20));
//        map1.put("Lilei",34);
        System.out.println(map1);
    }

    //java9新特性九：InputStream的新方法:tranferTo()
    @Test
    public void test5() {
        ClassLoader classLoader = this.getClass().getClassLoader();

        try(InputStream is = classLoader.getResourceAsStream("hello.txt");
            FileOutputStream os = new FileOutputStream("src\\hello1.txt");){
            is.transferTo(os);// 把输入流中的所有数据直接自动地复制到输出流中
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
